package treeviewer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;

public class UsingTree {

	private static String[] treeItemString;
	private static TreeColumn[] treeItemColumn;
	private static Tree tree = null;

	public static void main(String[] args) {
		Display display = Display.getDefault();

		Shell sShell = new Shell();
		sShell.setLayout(new FillLayout());
		sShell.setText("Using Tree");
		sShell.setSize(new Point(300, 200));
		tree = new Tree(sShell, SWT.FULL_SELECTION | SWT.CHECK);
		initPopup();
		tree.setHeaderVisible(true);
		tree.setLinesVisible(true);
		TreeColumn treeColumn = new TreeColumn(tree, SWT.NONE);
		treeColumn.setWidth(100);
		treeColumn.setText("Column 1");

		TreeColumn treeColumn1 = new TreeColumn(tree, SWT.NONE);
		treeColumn1.setWidth(100);
		treeColumn1.setText("Column 2");

		TreeColumn treeColumn2 = new TreeColumn(tree, SWT.NONE);
		treeColumn2.setWidth(100);
		treeColumn2.setText("Column 3");

		TreeItem root1 = new TreeItem(tree, SWT.NONE);
		root1.setText(new String[] { "Root 1", "Root 1 Content", "row 1" });
		TreeItem root2 = new TreeItem(tree, SWT.NONE);
		root2.setText(new String[] { "Root 2", "Root 2 Content", "row 2" });
		TreeItem child1 = new TreeItem(root1, SWT.NONE);
		child1.setText(new String[] { "Child 1", "Child 1 1", "Child 1 2" });
		TreeItem child2 = new TreeItem(root1, SWT.NONE);
		child2.setText(new String[] { "Child 2", "Child 2 1", "Child 2 2" });
		TreeItem child3 = new TreeItem(root2, SWT.NONE);
		child3.setText(new String[] { "Child 3", "Child 3 1", "Child 3 2" });
		tree.addSelectionListener(new SelectionListener() {

			public void widgetSelected(SelectionEvent e) {
				TreeItem selectedObj = (TreeItem) e.item;
				Tree myTree = (Tree) e.getSource();
				TreeColumn[] myTreeColumn = myTree.getColumns();
				treeItemColumn = myTree.getColumns();
				treeItemString = new String[treeItemColumn.length];

				for (int i = 0; i < myTreeColumn.length; i++) {
//					TreeColumn treeColumn = myTreeColumn[i];
					treeItemString[i] = selectedObj.getText(i);
				}
			}

			public void widgetDefaultSelected(SelectionEvent e) {

			}
		});

		tree.addMouseListener(new MouseListener() {
			public void mouseUp(MouseEvent e) {
				// if (e.button == 3){
//				TreeItem selected = ((Tree) e.getSource()).getItem(new Point(e.x, e.y));
				// if(selected != null)
				// System.out.println(e.x);
				int k = 0, j = 0;
				System.out.println(k);
				System.out.println(j);
				if (treeItemColumn != null) {
					for (; j < treeItemColumn.length; j++) {
						TreeColumn t = treeItemColumn[j];
						k = k + t.getWidth();
						if (k > e.x) {
							break;
						}
					}

				}
				if (j >= treeItemString.length) {
					return;
				}
				System.out.println(treeItemString[j]);
				// }
			}

			public void mouseDown(MouseEvent e) {
			}

			public void mouseDoubleClick(MouseEvent e) {
			}
		});

		sShell.open();
		sShell.layout();

		while (!sShell.isDisposed())
			if (!display.readAndDispatch())
				display.sleep();
	}

	private static void initPopup() {

		Menu menu = new Menu(tree);
		MenuItem newItem = new MenuItem(menu, SWT.PUSH);
		newItem.setText("sad");
		MenuItem newMemberItem = new MenuItem(menu, SWT.PUSH);

		newMemberItem.setText("asd");
		MenuItem editItem = new MenuItem(menu, SWT.PUSH);
		editItem.setText("sad");
		MenuItem deleteItem = new MenuItem(menu, SWT.PUSH);

		deleteItem.setText("asd");
		tree.setMenu(menu);

	}

}